package producers;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

import net.sf.json.JSONObject;
import play.libs.F.Promise;
import play.libs.WS;
import play.libs.WS.HttpResponse;
import play.libs.WS.WSRequest;
import utils.UrlUtil;
import utils.WSUtil;

/**
 * 账户信息交互接口定义
 * @author jinchaoyang
 */
public class AccountProducer implements IAccountProducer {
    
    private final static String BASE_URL="http://www.ucpaas.com/";

    public String sid;
    
    public String token;
    
    public AccountProducer(String sid,String token){
        this.sid = sid;
        this.token = token;
    }
    
    
    
    @Override
    public Map<String,String> create(String sid, String token, String appName, String nbr,String notifyUrl,
            Map<String, String> options) {
        Map<String,String> _params = new HashMap<String,String>();
        _params.put("nbr",nbr);
        _params.put("appName", appName);
        _params.put("notifyCallback", notifyUrl);
        _params.put("op", "ivr");
        if(null!=options){
            _params.putAll(options);
        }
        JsonObject obj = WSUtil.post(BASE_URL+Action.CREATE.getUrl(), sid, token, _params);
        Map<String,String> result = parseResult(obj); 
        return result;
        
    }

    @Override
    public Map<String,String> update(String sid, String token, String appId,String nbr,String notifyUrl, Map<String, String> options) {
        Map<String,String> _params = new HashMap<String,String>();
        _params.put("appId", appId);
        _params.put("notifyCallback", notifyUrl);
        _params.put("nbr", nbr);
        _params.put("op", "ivr");
        if(null!=options){
            _params.putAll(options);
        }
       JsonObject obj = WSUtil.post(BASE_URL+Action.UPDATE.getUrl(), sid, token, _params);
       Map<String,String> result = parseResult(obj);   
       return result;
    }

    @Override
    public Map<String,String> destroy(String sid, String token, String appId) {
       Map<String,String> _params = new HashMap<String,String>();
       _params.put("appId", appId);
       _params.put("op", "ivr");
       JsonObject obj = WSUtil.post(BASE_URL+Action.DESTROY.getUrl(), sid, token, _params);
       Map<String,String> result = parseResult(obj);  
       return result;
    }

    
    private Map<String,String> parseResult(JsonObject obj){
        Map<String,String> result = new HashMap<String,String>();
        result.put("rcode",obj.get("respCode").getAsString());
        result.put("msg",  obj.get("respMsg").getAsString());
        if(obj.has("app")){
            String _appId = obj.getAsJsonObject("app").get("appId").getAsString();
            result.put("appId",_appId);
        }else{
            result.put("appId", "");
        }
        return result;
    }
    

    
    enum Action{
        CREATE("/maap/ipcc/app/add"),
        UPDATE("/maap/ipcc/app/edit"),
        DESTROY("/maap/ipcc/app/del");
       
        private String url;
       
        private Action(String url){
            this.url = url;
        }
        
        public String getUrl(){
            return this.url;
        }
    }

}
